package com.tiza.xiaoyoubao.ui.fragment;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.orhanobut.hawk.Hawk;
import com.squareup.picasso.Picasso;
import com.tianze.library.base.BaseFragment;
import com.tiza.xiaoyoubao.R;
import com.tiza.xiaoyoubao.app.Constants;
import com.tiza.xiaoyoubao.app.UIHelper;
import com.tiza.xiaoyoubao.entity.*;
import com.tiza.xiaoyoubao.net.ApiHttpClient;
import com.tiza.xiaoyoubao.utils.DateUtils;

import butterknife.BindView;
import butterknife.OnClick;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;

/**
 * Created by mapple on 17/5/22.
 * 手机浏览任务操作任务页
 */

public class BrowseMpTask2Fragment extends BaseFragment {

    //商家ID
    @BindView(R.id.tv_sellerId)
    TextView tv_sellerId;


    @BindView(R.id.iv_image)
    ImageView iv_image;


    //搜索价格
    @BindView(R.id.tv_cJPrice)
    TextView tv_cJPrice;
    //操作状态
    @BindView(R.id.tv_taskState)
    TextView tv_taskState;
    //商家要求
    @BindView(R.id.tv_sellerRequire)
    TextView tv_sellerRequire;
    @BindView(R.id.tv_time)
    TextView tv_time;
    //编号
    @BindView(R.id.tv_taskId)
    TextView tv_taskId;
    //买号
    @BindView(R.id.tv_buyerId)
    TextView tv_buyerId;
    //倒计时
    @BindView(R.id.tv_countDwon)
    TextView tv_countDwon;
    //状态
//    @BindView(R.id.//tv_state)
//    TextView //tv_state;
    @BindView(R.id.bt_operationTask)
    Button bt_operationTask;
    @BindView(R.id.bt_CancelTask)
    Button bt_CancelTask;
    @BindView(R.id.bt_complaintTask)
    Button bt_complaintTask;
    //完成状态
    @BindView(R.id.tv_taskCompleteState)
    TextView tv_taskCompleteState;
    //获得佣金
    @BindView(R.id.tv_getCommission)
    TextView tv_getCommission;
    @BindView(R.id.view1)
    View view1;
    @BindView(R.id.view2)
    View view2;

    @BindView(R.id.iv1)
    View iv1;

    @BindView(R.id.iv2)
    View iv2;

    @BindView(R.id.iv3)
    View iv3;

    @BindView(R.id.iv5)
    View iv5;

    @BindView(R.id.iv6)
    View iv6;

    @BindView(R.id.iv7)
    View iv7;

    @BindView(R.id.iv_2)
    ImageView iv_2;
    @BindView(R.id.iv_3)
    ImageView iv_3;

    Bundle args;
    private String state, taskTime;

    Timer timer = new Timer();
    DateFormat df;
    Date date1;
    Date date2;
    long diff;


    private BrowseTaskOrderInfo browseTaskOrderInfo;
    private BrowseJDTaskOrderInfo browseJDTaskOrderInfo;
    private BrowseMGJTaskOrderInfo browseMGJTaskOrderInfo;
    private BrowseMLSTaskOrderInfo browseMLSTaskOrderInfo;
    private BrowseTMTaskOrderInfo browseTMTaskOrderInfo;
    private BrowseCompleteInfo browseCompleteInfo;
    private BrowseOperateInfo browseOperateInfo;
    private BrowseConfirmInfo browseConfirmInfo;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        args = getArguments();
        state = args.getString("state");
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_browse_mp_task2;
    }

    @Override
    protected void initData() {
    }

    @Override
    protected void initView() {
        switch (state) {
            case "browseOperate":
                BrowseOperate();
                break;
            case "confirm":
                BrowseConfirm();
                break;
            case "browseComplate":
                BrowseComplate();
                break;
            case "browseMgj":
                BrowseMGJ();
                break;
            case "browseMls":
                BrowseMLS();
                break;
            case "browseJd":
                BrowseJd();
                break;
            case "browseTm":
                BrowseTm();
                break;
            case "browseTb":
                BrowseTaoBao();
                break;
            default:
                break;
        }
    }


    @OnClick({R.id.view_go, R.id.bt_operationTask, R.id.bt_complaintTask, R.id.bt_CancelTask})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.view_go:
                UIHelper.showBrowseMpTaskDetail2(mActivity, args);
                break;
            case R.id.bt_operationTask:
                if (state.equals("browseOperate")) {
                    args.putString("way", "2");
                    args.putSerializable("info", browseOperateInfo);
                } else if (state.equals("browseTb")) {
                    args.putString("way", "1");
                }
                UIHelper.showBrowseOperationTask(mActivity, args);
                finish();
                break;
            case R.id.bt_complaintTask:
                UIHelper.showComplain(mActivity);
                finish();
                break;
            case R.id.bt_CancelTask:
                if (state.equals("browseOperate")) {
                    args.putString("way", "3");
                    args.putSerializable("info", browseOperateInfo);
                } else if (state.equals("browseTb")) {
                    args.putString("way", "5");
                } else if (state.equals("confirm")) {
                    args.putString("way", "4");
                    args.putSerializable("info", browseConfirmInfo);
                }
                UIHelper.showCancleTask(mActivity, args);
                break;
            default:
                break;
        }
    }


    TimerTask task = new TimerTask() {
        @Override
        public void run() {

            mActivity.runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    try {
                        date1 = df.parse(df.format(new Date()));
                        date2 = df.parse(taskTime);
                    } catch (ParseException e) {
                        e.printStackTrace();
                    }
                    diff = (date2.getTime() - date1.getTime()) / 1000;
                    int mm = (int) diff;
                    String time = change(mm);
                    tv_countDwon.setText("提交倒计时:" + time);
                    if (diff <= 0) {
                        timer.cancel();
                        //cancelTask();
                        tv_countDwon.setVisibility(View.GONE);
                    }
                }
            });
        }
    };

    /*
    * 将秒数转为时分秒
    * */

    public String change(int second) {
        int h = 0;
        int d = 0;
        int s = 0;
        String hour = null;
        String minute = null;
        String sec = null;
        String m = "0";
        int temp = second % 3600;
        if (second > 3600) {
            h = second / 3600;
            if (temp != 0) {
                if (temp > 60) {
                    d = temp / 60;
                    if (d < 10) {
                        minute = "0" + d;
                    } else {
                        minute = String.valueOf(d);
                    }
                    if (temp % 60 != 0) {
                        s = temp % 60;
                        if (s < 10) {
                            sec = "0" + s;
                        } else {
                            sec = String.valueOf(s);
                        }
                    }
                } else {
                    s = temp;
                    if (s < 10) {
                        sec = "0" + s;
                    } else {
                        sec = String.valueOf(s);
                    }
                }
            }
        } else {
            d = second / 60;
            if (d < 10) {
                minute = "0" + d;
            } else {
                minute = String.valueOf(d);
            }
            if (second % 60 != 0) {
                s = second % 60;
                if (s < 10) {
                    sec = "0" + s;
                } else {
                    sec = String.valueOf(s);
                }
            }
        }
        return "0" + h + ":" + minute + ":" + sec + "";
    }

    public void onResume() {
        super.onResume();
        timer.cancel();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        timer.cancel();
    }

    public void BrowseTaoBao() {
        browseTaskOrderInfo = (BrowseTaskOrderInfo) args.getSerializable("info");
        tv_sellerId.setText("商家ID:" + browseTaskOrderInfo.getUserId());
        tv_taskId.setText(browseTaskOrderInfo.getOrderid());
        tv_buyerId.setText(browseTaskOrderInfo.getAccount());
        tv_cJPrice.setText(browseTaskOrderInfo.getMobilePrice());
        tv_taskState.setText("任务状态：待操作");
        Picasso.with(mActivity).load(ApiHttpClient.URL2 + browseTaskOrderInfo.getImages()).placeholder(
                R.mipmap.rwcx_jiazaizhong).error(R.mipmap.rwcx_jiazaizhong).into(iv_image);
        tv_cJPrice.setText(browseTaskOrderInfo.getPrice());
        tv_taskState.setText("任务状态：待操作");
        //tv_state.setText("未完成");
        tv_taskCompleteState.setText("未完成");
        if (browseTaskOrderInfo.getRemarks().equals(" ")) {
            tv_sellerRequire.setText("商家要求:此商家无其他要求");
        } else {
            tv_sellerRequire.setText("商家要求:" + browseTaskOrderInfo.getRemarks());
        }
        tv_getCommission.setText(DateUtils.formateRate(browseTaskOrderInfo.getBaseCmi()));
//        df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//        taskTime = DateUtils.getDateToStringSec(browseOperateInfo.getEndTime());
//        timer.schedule(task, 1000, 1000);
    }

    public void BrowseJd() {
        browseJDTaskOrderInfo = (BrowseJDTaskOrderInfo) args.getSerializable("info");
        tv_sellerId.setText("商家ID:" + browseJDTaskOrderInfo.getUserId());
        tv_cJPrice.setText(browseJDTaskOrderInfo.getMobilePrice());
        tv_taskState.setText("任务状态：待操作");
        //tv_state.setText("未完成");
        tv_taskCompleteState.setText("未完成");
        if (browseJDTaskOrderInfo.getRemarks().equals(" ")) {
            tv_sellerRequire.setText("商家要求:此商家无其他要求");
        } else {
            tv_sellerRequire.setText("商家要求:" + browseJDTaskOrderInfo.getRemarks());
        }
        tv_getCommission.setText(DateUtils.formateRate(browseJDTaskOrderInfo.getBaseCmi()));
    }

    public void BrowseTm() {
        browseTMTaskOrderInfo = (BrowseTMTaskOrderInfo) args.getSerializable("info");
        tv_sellerId.setText("商家ID:" + browseTMTaskOrderInfo.getUserId());
        tv_cJPrice.setText(browseTMTaskOrderInfo.getMobilePrice());
        tv_taskState.setText("任务状态：待操作");
        //tv_state.setText("未完成");
        tv_taskCompleteState.setText("未完成");
        if (browseTMTaskOrderInfo.getRemarks().equals(" ")) {
            tv_sellerRequire.setText("商家要求:此商家无其他要求");
        } else {
            tv_sellerRequire.setText("商家要求:" + browseTMTaskOrderInfo.getRemarks());
        }
        tv_getCommission.setText(DateUtils.formateRate(browseTMTaskOrderInfo.getBaseCmi()));
    }

    public void BrowseMLS() {
        browseMLSTaskOrderInfo = (BrowseMLSTaskOrderInfo) args.getSerializable("info");
        tv_sellerId.setText("商家ID:" + browseMLSTaskOrderInfo.getUserId());
        tv_cJPrice.setText(browseMLSTaskOrderInfo.getMobilePrice());
        tv_taskState.setText("任务状态：待操作");
        //tv_state.setText("未完成");
        tv_taskCompleteState.setText("未完成");
        if (browseMLSTaskOrderInfo.getRemarks().equals(" ")) {
            tv_sellerRequire.setText("商家要求:此商家无其他要求");
        } else {
            tv_sellerRequire.setText("商家要求:" + browseMLSTaskOrderInfo.getRemarks());
        }
        tv_getCommission.setText(DateUtils.formateRate(browseMLSTaskOrderInfo.getBaseCmi()));
    }

    public void BrowseMGJ() {
        browseMGJTaskOrderInfo = (BrowseMGJTaskOrderInfo) args.getSerializable("info");
        tv_sellerId.setText("商家ID:" + browseMGJTaskOrderInfo.getUserId());
        tv_cJPrice.setText(browseMGJTaskOrderInfo.getMobilePrice());
        tv_taskState.setText("任务状态：待操作");
        //tv_state.setText("未完成");
        tv_taskCompleteState.setText("未完成");
        if (browseMGJTaskOrderInfo.getRemarks().equals(" ")) {
            tv_sellerRequire.setText("商家要求:此商家无其他要求");
        } else {
            tv_sellerRequire.setText("商家要求:" + browseMGJTaskOrderInfo.getRemarks());
        }
        tv_getCommission.setText(DateUtils.formateRate(browseMGJTaskOrderInfo.getBaseCmi()));
    }

    public void BrowseComplate() {
        bt_operationTask.setBackgroundResource(R.drawable.bg_radius_btn2);
        bt_operationTask.setEnabled(false);
        bt_CancelTask.setBackgroundResource(R.drawable.bg_radius_btn2);
        bt_CancelTask.setEnabled(false);
        browseCompleteInfo = (BrowseCompleteInfo) args.getSerializable("info");
        Picasso.with(mActivity).load(ApiHttpClient.URL2 + browseCompleteInfo.getImages()).placeholder(
                R.mipmap.rwcx_jiazaizhong).error(R.mipmap.rwcx_jiazaizhong).into(iv_image);
        tv_taskId.setText(browseCompleteInfo.getOrderid());
        tv_buyerId.setText(browseCompleteInfo.getAccount());
        tv_sellerId.setText("商家ID:" + browseCompleteInfo.getUserId());
        tv_cJPrice.setText(browseCompleteInfo.getPrice());
        tv_taskState.setText("任务状态：已完成");
        //tv_state.setText("已完成");
        tv_taskCompleteState.setText("已完成");
        if (browseCompleteInfo.getRemarks().equals(" ")) {
            tv_sellerRequire.setText("商家要求:此商家无其他要求");
        } else {
            tv_sellerRequire.setText("商家要求:" + browseCompleteInfo.getRemarks());
        }
        tv_getCommission.setText(DateUtils.formateRate(browseCompleteInfo.getBaseCmi()));
        view1.setBackgroundColor(getResources().getColor(R.color.steel_blue));
        view2.setBackgroundColor(getResources().getColor(R.color.steel_blue));
        iv_2.setImageResource(R.mipmap.rwxq_2);
        iv_3.setImageResource(R.mipmap.rwxq_3);
    }

    public void BrowseOperate() {
        browseOperateInfo = (BrowseOperateInfo) args.getSerializable("info");
        tv_sellerId.setText("商家ID:" + browseOperateInfo.getUserId());
        tv_taskId.setText(browseOperateInfo.getOrderid());
        tv_buyerId.setText(browseOperateInfo.getAccount());
        Picasso.with(mActivity).load(ApiHttpClient.URL2 + browseOperateInfo.getImages()).placeholder(
                R.mipmap.rwcx_jiazaizhong).error(R.mipmap.rwcx_jiazaizhong).into(iv_image);
        tv_cJPrice.setText(browseOperateInfo.getMobilePrice());
        tv_taskState.setText("任务状态：待操作");
        //tv_state.setText("未完成");
        tv_taskCompleteState.setText("未完成");
        if (browseOperateInfo.getRemarks().equals(" ")) {
            tv_sellerRequire.setText("商家要求:此商家无其他要求");
        } else {
            tv_sellerRequire.setText("商家要求:" + browseOperateInfo.getRemarks());
        }
        tv_getCommission.setText(DateUtils.formateRate(browseOperateInfo.getBaseCmi()));
        df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        taskTime = DateUtils.getDateToStringSec(browseOperateInfo.getEndTime());
        timer.schedule(task, 1000, 1000);
    }

    public void BrowseConfirm() {
        bt_operationTask.setBackgroundResource(R.drawable.bg_radius_btn2);
        bt_operationTask.setEnabled(false);
        bt_CancelTask.setBackgroundResource(R.drawable.bg_radius_btn2);
        bt_CancelTask.setEnabled(false);
        browseConfirmInfo = (BrowseConfirmInfo) args.getSerializable("info");
        tv_sellerId.setText("商家ID:" + browseConfirmInfo.getUserId());
        tv_taskId.setText(browseConfirmInfo.getOrderid());
        tv_buyerId.setText(browseConfirmInfo.getAccount());
        Picasso.with(mActivity).load(ApiHttpClient.URL2 + browseConfirmInfo.getImages()).placeholder(
                R.mipmap.rwcx_jiazaizhong).error(R.mipmap.rwcx_jiazaizhong).into(iv_image);
        tv_cJPrice.setText(browseConfirmInfo.getMobilePrice());
        tv_taskState.setText("任务状态：待确认");
        //tv_state.setText("未完成");
        tv_taskCompleteState.setText("未完成");
        if (browseConfirmInfo.getRemarks().equals(" ")) {
            tv_sellerRequire.setText("商家要求:此商家无其他要求");
        } else {
            tv_sellerRequire.setText("商家要求:" + browseConfirmInfo.getRemarks());
        }
        tv_getCommission.setText(DateUtils.formateRate(browseConfirmInfo.getBaseCmi()));
        view1.setBackgroundColor(getResources().getColor(R.color.steel_blue));
        iv_2.setImageResource(R.mipmap.rwxq_2);
    }

}
